// 基础统计信息
export function baseInfo(bibJSON) {
    console.info('\n\n\n\n====================基础统计信息 开始====================');
    console.log(`共有${bibJSON.length}条记录`);
    // 统计每种类型的数量
    let typeMap = {};
    for (let i = 0; i < bibJSON.length; i++) {
        let type = bibJSON[i].type;
        if (typeMap[type]) {
            typeMap[type]++;
        } else {
            typeMap[type] = 1;
        }
    }
    // 按照大小排序
    typeMap = Object.keys(typeMap).sort((a, b) => typeMap[b] - typeMap[a]).reduce((acc, cur) => {
        acc[cur] = typeMap[cur];
        return acc;
    }, {});
    console.table(typeMap);
    console.info('====================基础统计信息 结束====================');
}


// 检查字段的完整性,必须包含的字段有
// title, author, year, journal, volume, number, pages, doi
export function valid(bibJSON) {
    // book的必备字段
    let bookMustHaveFields = ['title', 'author', 'year', 'publisher'];
    let articleMustHaveFields = ['title', 'author', 'year', 'journal', 'volume', 'number', 'pages', 'doi'];
    console.info('\n\n\n\n====================字段完整性检查 开始====================');
    let hasError = false;
    // 循环检查完整性
    for (let i = 0; i < bibJSON.length; i++) {
        let item = bibJSON[i];
        // 如果是book类型
        if (item.type === 'book') {
            for (let j = 0; j < bookMustHaveFields.length; j++) {
                if (!item[bookMustHaveFields[j]]) {
                    console.log(`${bookMustHaveFields[j]} is missing in ${item.id}`);
                    hasError = true;
                }
            }
        }
        // 如果是article类型
        else if (item.type === 'article') {
            for (let j = 0; j < articleMustHaveFields.length; j++) {
                if (!item[articleMustHaveFields[j]]) {
                    console.log(`${articleMustHaveFields[j]} is missing in ${item.id}`);
                    hasError = true;
                }
            }
        }
    }
    console.info('====================字段完整性检查 结束====================');
}

// 检查引用情况
export function checkCite(bibs, article) {
    console.info('\n\n\n\n====================检查引用情况 开始====================');
    // 遍历bibd,看id是否出现在article内
    for (let i = 0; i < bibs.length; i++) {
        let id = bibs[i].id;
        if (article.indexOf(id) === -1) {
            console.log(`${id} 没被引用`);
        }
    }
    console.info('====================检查引用情况 结束====================');
}

// 检查是否有重复的内容
export function checkDuplicate(bibs) {
    console.info('\n\n\n\n====================检查重复内容 开始====================\n');
    let ids = [];
    for (let i = 0; i < bibs.length; i++) {
        let id = bibs[i].id;
        if (ids.indexOf(id) === -1) {
            ids.push(id);
        } else {
            console.log(`${id} 重复`);
        }
    }
    console.info('====================检查重复内容 结束====================');
}

// 检查是否有标题重复的情况
export function checkTitleDuplicate(bibs) {
    console.info('\n\n\n\n====================检查标题重复内容 开始====================\n');
    let titles = [];
    for (let i = 0; i < bibs.length; i++) {
        let title = bibs[i].title;
        // 去掉标题里面的 {} 和 \"
        title = title.replace(/{/g, '');
        title = title.replace(/}/g, '');
        title = title.replace(/\"/g, '');
        // 全部转为小写
        title = title.toLowerCase();
        if (titles.indexOf(title) === -1) {
            titles.push(title);
        } else {
            console.log(`${title} 重复`);
        }
    }
    console.info('====================检查标题重复内容 结束====================');
}

// 统计每个期刊的引用次数,并且计算百分比
export function countJournal(bibs, article) {
    console.info('\n\n\n\n====================统计每个期刊的引用次数 开始====================');
    let journalMap = {};
    for (let i = 0; i < bibs.length; i++) {
        let journal = bibs[i].journal;
        let id = bibs[i].id;
        if (article.indexOf(id) === -1) {
            continue;
        }
        if (journalMap[journal]) {
            journalMap[journal]++;
        } else {
            journalMap[journal] = 1;
        }
    }
    // 按照次数从高到低排序
    journalMap = Object.keys(journalMap).sort((a, b) => journalMap[b] - journalMap[a]).reduce((acc, cur) => {
        acc[cur] = journalMap[cur];
        return acc;
    }, {});
    // 统计计算百分比
    let total = bibs.length;
    for (let key in journalMap) {
        journalMap[key] = {
            count: journalMap[key],
            percent: (journalMap[key] / total * 100).toFixed(2) + '%'
        }
    }

    // 用格式化的表格类型输出
    console.table(journalMap);
    console.info('====================统计每个期刊的引用次数 结束====================');
}

// 统计年份
export function countYear(bibs) {
    console.info('\n\n\n\n====================统计年份 开始====================');
    let yearMap = {};
    for (let i = 0; i < bibs.length; i++) {
        let year = bibs[i].year;
        if (yearMap[year]) {
            yearMap[year]++;
        } else {
            yearMap[year] = 1;
        }
    }
    // 按照次数从高到低排序
    yearMap = Object.keys(yearMap).sort((a, b) => a - b).reduce((acc, cur) => {
        acc[cur] = yearMap[cur];
        return acc;
    }, {});
    // 用格式化的表格类型输出
    console.table(yearMap);
    console.info('====================统计年份 结束====================');
}

// ========================================================================================================================
// Latex内容检查部分

// 检查tex内容中是否有中文的符号
export function checkChinese(tex) {
    console.info('\n\n\n\n====================检查中文符号 开始====================');
    let chineseReg = /[\u4e00-\u9fa5]/g;
    let chineseArr = tex.match(chineseReg);
    if (chineseArr) {
        console.log('存在中文符号');
    }
    console.info('====================检查中文符号 结束====================');
}